package com.ellecity06.cwhiskymall.receiver

import android.content.Context
import android.content.Intent
import android.os.Bundle
import cn.jpush.android.api.CmdMessage
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.ellecity06.common.utils.gson
import com.ellecity06.common.utils.logD
import com.ellecity06.cwhiskymall.bean.push.PushTopGoodModel
import com.ellecity06.cwhiskymall.constant.BusConstants
import com.ellecity06.cwhiskymall.ui.MainActivity
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author ellecity06
 * @time 2025/1/8 14:42
 * @des
 */
class JPushBroadcastReceiver : JPushMessageReceiver() {
    private val TAG = "JPushBroadcastReceiver"

    companion object {
        /**
         * 直播开播通知
         */
        private const val TYPE_LIVE_OPEN = "1"

        /**
         * 直播关闭通知
         */
        private const val TYPE_LIVE_CLOSE = "2"

        /**
         * 直播商品讲解
         */
        private const val TYPE_LIVE_TOP_GOOD = "4"
        /**
         * 直播商品讲解
         */
        private const val TYPE_LIVE_TOP_GOOD_STOP = "5"
    }

    override fun onMessage(context: Context, customMessage: CustomMessage) {
        logD("推送的自定义消息=$customMessage", tag = TAG)
//        LiveEventBus.get<Boolean>(BusConstants.LIVE_STATUS_EVENT).post(true)
        // 收到自定义消息发送站内消息通知页面
        try {
            when (customMessage.contentType) {
                TYPE_LIVE_OPEN -> {
                    LiveEventBus.get<Boolean>(BusConstants.LIVE_STATUS_EVENT).post(true)
                }

                TYPE_LIVE_CLOSE -> {
                    LiveEventBus.get<Boolean>(BusConstants.LIVE_STATUS_EVENT).post(false)
                }
                TYPE_LIVE_TOP_GOOD_STOP,
                TYPE_LIVE_TOP_GOOD -> {
                    val topGoodMessage = gson.fromJson(customMessage.extra, PushTopGoodModel::class.java)
                    LiveEventBus.get<PushTopGoodModel>(BusConstants.LIVE_TOP_GOOD).post(topGoodMessage)
                }
//                TYPE_LIVE_TOP_GOOD_STOP->{
////                    val topGoodMessage = gson.fromJson(customMessage.extra, PushTopGoodModel::class.java)
//                    LiveEventBus.get<Boolean>(BusConstants.LIVE_TOP_GOOD_STOP).post(true)
//                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    override fun onNotifyMessageOpened(context: Context, message: NotificationMessage) {
        logD("推送通知消息被打开了=$message", tag = TAG)
        try {
            //打开自定义的Activity
            val i = Intent(context, MainActivity::class.java)
            val bundle = Bundle()
            bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE, message.notificationTitle)
            bundle.putString(JPushInterface.EXTRA_ALERT, message.notificationContent)
            i.putExtras(bundle)
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
            context.startActivity(i)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
        }
    }

    override fun onMultiActionClicked(context: Context?, intent: Intent) {
        logD("[onMultiActionClicked] 用户点击了通知栏按钮", tag = TAG)
        val nActionExtra = intent.extras!!.getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA) ?: return

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。

    }

    override fun onNotifyMessageArrived(context: Context?, message: NotificationMessage) {
        logD("[onNotifyMessageArrived] $message", tag = TAG)
    }

    override fun onNotifyMessageDismiss(context: Context?, message: NotificationMessage) {

        logD("[onNotifyMessageDismiss] $message", tag = TAG)
    }

    override fun onRegister(context: Context, registrationId: String) {
        logD("[onRegister] $registrationId", tag = TAG)


    }

    override fun onConnected(context: Context?, isConnected: Boolean) {
        logD("[onConnected] $isConnected", tag = TAG)
    }

    override fun onCommandResult(context: Context?, cmdMessage: CmdMessage) {
        logD("[onCommandResult] $cmdMessage", tag = TAG)
    }

    override fun onTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
//        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage)
        logD("[onTagOperatorResult] jPushMessage:$jPushMessage", tag = TAG)
        super.onTagOperatorResult(context, jPushMessage)
    }

    override fun onCheckTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
//        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage)
        logD("[onCheckTagOperatorResult] jPushMessage:$jPushMessage", tag = TAG)
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    override fun onAliasOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
//        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage)
        logD("[onAliasOperatorResult] jPushMessage:$jPushMessage", tag = TAG)
        super.onAliasOperatorResult(context, jPushMessage)
    }

    override fun onMobileNumberOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
//        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage)
        logD("[onMobileNumberOperatorResult] jPushMessage:$jPushMessage", tag = TAG)
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

    override fun onNotificationSettingsCheck(context: Context?, isOn: Boolean, source: Int) {
        super.onNotificationSettingsCheck(context, isOn, source)
        logD("[onNotificationSettingsCheck] isOn:$isOn,source:$source", tag = TAG)
    }
}